<?php
/*
	账户支付
	管理端支付接口
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'pay';
$return=array();
$setting=core_setting('pay');
if($_RQ['op']=='copy'){
	if($_SESSION['container']=='wechat'){
		$id=intval($_RQ['id']);
		$pay_url=manage_url('core/member.pay/copy/id='.$id,true);
		$return['pay_url']=$pay_url;
		exi($return);
	}else{
		$_RQ['op']='pay';
	}
	
}
if($_RQ['op']=='pay'){	
	$id=intval($_RQ['id']);
	if(!$id){
		exi('发起支付失败！请先选择要支付的订单1！','error','home.welcome/');
	}
	$pay=pdo_get('core_account_pay_log',array('id'=>$id));	
	if(!$pay){
		exi('找不到要支付的订单！','error','home.welcome/');
	}elseif($pay['no_type']){
		$pay['no_type']=json_decode($pay['no_type'],true);
	}
	if($pay['status']){
		exi('订单已支付！','error','home.welcome/');
	}
	$pay_types=array('yinlian','alipay','wechat');
	if($_SESSION['uid']){
		$pay_types[]='credit';
	}
	/*
	if($_SESSION['container']!='wechat'){
		//不在微信环境，添加支付宝支付
		$pay_types[]='alipay';
	}
	if($_SESSION['container']!='alipay'){
		//不在支付宝环境，添加微信支付
		$pay_types[]='wechat';
	}*/
	foreach($pay_types as $key=>$type){
		if($setting[$type]!=1){
			unset($pay_types[$key]);
		}
	}
	if($pay['no_type']){
		$pay_types=array_diff($pay_types,$pay['no_type']);
	}
	$return=array(
		'id'=>$pay['id'],
		'tid'=>$pay['ordersn'],
		'fee'=>$pay['fee'],
		'pay_types'=>array_values($pay_types),
		'iscredit'=>$setting['credit']
	);
	if($_SESSION['uid']){
		//$return['mid']=$_SESSION['uid'];
		$return['credit']=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'credit2');
	}
	if($pay['pay_type'] && in_array($pay['pay_type'],$pay_types)){
		$return['pay_type']=$pay['pay_type'];
	}
	if(!$return['pay_types'] && !$return['pay_type']){
		exi('没有可用的支付方式，请联系系统管理员！','error');
	}
	if(count($return['pay_types'])==1){
		$return['pay_type']=$return['pay_types']['0'];
	}
	exi($return);
}
if($_RQ['op']=='alipay'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$order=pdo_get('core_account_pay_log',array('id'=>$id));
	if(!$order){
		exit('支付出错：要支付的订单不存在！');
	}
	
	$data=array(
		'subject'=>$order['title'],
		'total_fee'=>$order['fee'],
		'return_url'=>manage_url('core/account.pay/pay_alipay_result',true),
		'notify_url'=>SITEROOT.'manage/payresult/alipay.php',
		'out_trade_no'=>$order['ordersn'],
	);
	$html=cfc('core')->getalipay($data);
	exi(base64_encode($html));
	
}
if($_RQ['op']=='wechat'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_account_pay_log',array('id'=>$id));
	$params=array(
		'title'=>$pay_log['title'],
		'fee'=>$pay_log['fee'],
		'tid'=>$pay_log['ordersn'],
		'notify_url'=>SITEROOT.'manage/payresult/wechat.php',
	);
	$config=array(
		'appid'=>$setting['appid'],
		'mch_id'=>$setting['mch_id'],
		'key'=>$setting['api_key'],
	);
	if($_SESSION['container']=='wechat'){
		
		$wechat_options=cfc('wechat_pay',$config)->jsapi_pay($params);;
		exi($wechat_options);
	}else{
		//非微信环境，发起手机网页支付
		$murl=cfc('wechat_pay',$config)->h5_pay($params);
		exi($murl);
	}	
}
if($_RQ['op']=='wxapp_pay'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_account_pay_log',array('id'=>$id));
	$params=array(
		'title'=>$pay_log['title'],
		'fee'=>$pay_log['fee'],
		'tid'=>$pay_log['ordersn'],
		'success_url'=>$pay_log['success_url'],
		'error_url'=>$pay_log['error_url'],
	);
	if($params['fee']==0){
		exi('订单金额不能为零！','error');
	}
	$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['appgid']),array('appid','mchid','paykey'));
	if(!$wxapp || !$wxapp['appid'] || !$wxapp['mchid'] || !$wxapp['paykey']){
		exi('发起支付失败！小程序支付参数设置错误！','error');
	}
	$setting=array(
		'appid'=>$wxapp['appid'],
		'mch_id'=>$wxapp['mchid'],
		'key'=>$wxapp['paykey'],
	);	
	$res=cfc('wechat_pay',$setting)->wxapp_pay($params);
	exi($res);
}
if($_RQ['op']=='credit'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_account_pay_log',array('id'=>$id));
	$credit2=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'credit2');
		if($pay_log['fee']>$credit2){
			exi('余额不足！','error');
		}
		$credit2=$credit2-$pay_log['fee'];
		pdo_update('core_accounts',array('credit2'=>$credit2),array('uniacid'=>$_SESSION['uniacid']));
		pdo_update('core_account_pay_log',array('status'=>1,'pay_type'=>'credit'),array('id'=>$id));		
		$log=array(
			'uniacid'=>$_SESSION['uniacid'],
			'type'=>2,
			'is_add'=>0,
			'fee'=>$pay_log['fee'],
			'remark'=>'支付订单【'.$pay_log['ordersn'].'】：'.$pay_log['title'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('core_account_credit_log',$log);
		$log_id=pdo_insertid();
		
	//此处模拟微信支付等进行异步支付结果通知~
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'uid'=>$_SESSION['uid'],
		'timestamp'=>TIMESTAMP,
		'back_url'=>$pay_log['back_url'],
		'pay_result'=>ijson_encode(array('id'=>$pay_log['order_id'],'status'=>1,'pay_type'=>'credit','fee'=>$pay_log['fee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['ordersn'],'title'=>$pay_log['title'])),
	);
	$data['sign']=get_sign($data);
	$res=cfc('http')->ihttp_request(SITEROOT.'manage/payresult/credit_notify.php',ijson_encode($data));
	exi('success');
}
if($_RQ['op']=='yinlian'){
	
}
